package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.dto.JudgesManagerDTO;
import com.cqut.model.JudgesManager;
import com.cqut.dto.LimitShowDTO;
import com.cqut.service.IJudgesManagerService;
import com.cqut.util.BeanUtil;
import com.cqut.util.ResponseUtil;


@RestController
@RequestMapping("/api/judgesManager")
public class JudgesManagerController {
    @Resource(name="judgesManagerService")
    private IJudgesManagerService judgesManagerService;

    /**
     * 刷新评委列表
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors, String judgeName) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();
     //   judgesManagerService.
       // List<Map<String, Object>> judgesManagerList = judgesManagerService.loadMaps(null, null, null, null, limitShowDTO.getCurPage(), limitShowDTO.getLimit());
        List<Map<String, Object>> judgesManagerList = judgesManagerService.findJudgesWithLimit(judgeName, limitShowDTO);
        long count = judgesManagerService.findCountOfJudges(judgeName);
        System.out.println(judgesManagerList.toString());
        result.put("rows", judgesManagerList);
        result.put("total", count);
        System.out.println(judgeName);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    /**
     * 
     * 得到职称和学历列表
     */
    @RequestMapping(value = "/EduAndTitlelist", method = RequestMethod.GET)
    public ResponseEntity<String> EduAndTitlelist(
    		@Validated
            LimitShowDTO limitShowDTO,BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String,Object>> getEducation = judgesManagerService.getEducationList();
        List<Map<String,Object>> getTitle = judgesManagerService.getTitle();

        result.put("education", getEducation);
        result.put("title", getTitle);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    /**
     * 根据ID查找评委详情
     * @param judgesId
     * @return
     */
    @RequestMapping(value = "/getJudgeDetail/{judgesId}", method = RequestMethod.GET)
    public ResponseEntity<String> show(@PathVariable("judgesId") String judgesId) {
    	System.out.println(judgesId);
        Map<String, Object> judgesManagerModel = judgesManagerService.findMap(judgesId);
        System.out.println(judgesManagerModel);
        return ResponseUtil.getResEntityForGetAndJson(judgesManagerModel);
    }

    /**
     * 新增评委
     * @param judgesManagerDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/getJudge", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        JudgesManagerDTO judgesManagerDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        JudgesManager judgesManagerModel = judgesManagerDTO.toModel();
        
        return ResponseUtil.getResEntityForPPP(judgesManagerService.save(judgesManagerModel));
    }

    /**
     * 根据ID更新评委信息
     * @param judgesId
     * @param judgesManagerDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/updateJudge", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
        @Validated
        JudgesManagerDTO judgesManagerDTO, BindingResult errors) {

        JudgesManager judgesManagerModel = judgesManagerDTO.toModel();
        judgesManagerModel.setJudgesId(judgesManagerDTO.getJudgesId());
        return ResponseUtil.getResEntityForPPP(judgesManagerService.update(judgesManagerModel));
    }

    @RequestMapping(value = "/{judgesId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("judgesId") String judgesId) {
        return ResponseUtil.getResEntityForDel(judgesManagerService.delete(judgesId));
    }

    @RequestMapping(value = "/deletes", method = RequestMethod.POST)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
    	if(judgesManagerService.batchDelete(primaryKeys)){
    		return ResponseUtil.getResEntityForDelWhenSuccess();
    		
    	}
        return ResponseUtil.getResEntityForDelWhenFail();
    }
}
